package tests;

import common.Utils;
import org.bouncycastle.pqc.crypto.newhope.NHSecretKeyProcessor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * @Author xuejiao
 * Data 2025/5/30 10:55
 */
public class LoginPage extends Utils {
    public static String url = "http://localhost:8080/blogin.html";
    public LoginPage() {
        super(url);
    }

    // 登录页面加载是否正确
    public void loginPageRight() throws InterruptedException {
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("body > div > div.login-container.col-sm-6.col-md-6.col-lg-5.col-xl-5 > div.tab-box > span.active.tab-span"));
        driver.findElement(By.cssSelector("body > div > div.login-container.col-sm-6.col-md-6.col-lg-5.col-xl-5 > div.tab-box > span.tab-span.active"));
        driver.findElement(By.cssSelector("#loginForm > button"));

    }

    //成功登录
    public void loginSuccess() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
        driver.navigate().refresh();

        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("15172970453");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        Thread.sleep(3000);

        //检查点击登录之后是否登录成功，找页面的活动列表
        driver.findElement(By.cssSelector("body > div.cont-box > div.sidebar > ul > li:nth-child(2) > ul > li:nth-child(2) > a"));
        //检查是否登录成功，检查页面标题
        String expect = driver.getTitle();
        assert expect.equals("后台管理");

    }

    //登录失败
    public void loginFail() throws InterruptedException {
        //方式一：通过clear保证输入框没有文本
//        driver.findElement(By.cssSelector("#phoneNumber")).clear();
//        driver.findElement(By.cssSelector("#password")).clear();


        //方式2：navigate.refresh() 重新刷新页面，输入框会被清空
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));

        //driver.navigate().back();
        //driver.navigate().refresh();

        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("15172970453");
        driver.findElement(By.cssSelector("#password")).sendKeys("1234567");
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        Thread.sleep(3000);

        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        assert alertText.equals("登录失败！密码错误");
        alert.accept();

    }
}
